package com.estronger.shopmanage.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtils {

    /**
     * yyyy年MM月dd日hh:mm:ss yyyy年MM月
     **/
    public static String getSystime(String formattime) {
        String systime;
        SimpleDateFormat dateFormat = new SimpleDateFormat(formattime);
        systime = dateFormat.format(new Date(System.currentTimeMillis()));
        return systime;
    }

    /**
     * @param currentTime
     * @param format      时间格式
     * @return 格式化的时间
     */
    public static String formatTime(long currentTime, String format) {
        if (format == null || TextUtils.isEmpty(format))
            format = "yyyy-MM-dd HH:mm";
        SimpleDateFormat formatter = new SimpleDateFormat(format);// HH:mm:ss
        // "yyyy-MM-dd HH:mm"
        Date date = new Date(currentTime);
        System.out.println(formatter.format(date));
        return formatter.format(date);
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.equals("null")) {
            return "";
        }
        if (format == null || TextUtils.isEmpty(format))
            format = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return
     */
    public static String timeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    /**
     * 是否当天
     *
     * @param date
     * @return
     */
    public static boolean isToday(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("M月dd日");
        Date today = new Date();
        Date date1 = null;
        try {
            date1 = format.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String dateFormat1 = format2.format(date1);
        String dateFormat2 = format2.format(today);
        return dateFormat1.equals(dateFormat2);
    }

    /**
     * 计算传入的时间与系统当前时间的差值
     *
     * @param date 传入的时间
     * @return
     */
    public static String chaZhi(String date) {
        if (TextUtils.isEmpty(date))
            return "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(date);
            long diff = d1.getTime() - System.currentTimeMillis();// 这样得到的差值是微秒级别

            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24))
                    / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
                    * (1000 * 60 * 60))
                    / (1000 * 60);
            if (days < 0 || hours < 0 || minutes < 0) {
                return ("已延期(" + Math.abs(days) + "天" + Math.abs(hours) + "小时"
                        + Math.abs(minutes) + "分" + ")");
            }
            return ("还剩(" + "" + days + "天" + hours + "小时" + minutes + "分" + ")");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 时间去掉秒
     **/
    public static String cutsecond(String date) {
        return date.substring(0, date.lastIndexOf(":"));
    }


    /**
     * 时间日期转long
     **/
    public static long getTimeLong(String time, String format) {
        try {
            SimpleDateFormat s = new SimpleDateFormat(format);
            Date d = s.parse(time);
            return d.getTime();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 比较两个时间的大小
     **/
    public static boolean diffTime(String time1, String time2, String formatter) {
        try {
            long l1 = getTimeLong(time1, formatter);
            long l2 = getTimeLong(time2, formatter);
            if (l1 > l2) {
                return false;
            } else if (l1 <= l2) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    /**比较两个日期的间隔**/
    public static long diffSubtractTime(String time1, String time2, String formatter) {
        try {
            long l1 = getTimeLong(time1, formatter);
            long l2 = getTimeLong(time2, formatter);
            return l1 - l2;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param day        把日期往后增加一天.整数往后推,负数往前移动 -1 昨天 -2 前天 -3大前天
     * @param formattime 日期格式
     **/
    public static String getSystemPreDate(int day, String formattime) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(formattime);
            Date d = dateFormat.parse(getSystime(formattime));//取时间
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(d);
            calendar.add(calendar.DATE, day);//把日期往后增加一天.整数往后推,负数往前移动
            d = calendar.getTime();   //这个时间就是日期往后推一天的结果
            return dateFormat.format(d);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取当前日期是星期几
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static int getWeekOfDate(Date dt) {
        //String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;//weekDays[w];
    }

    public static String getWeekDesc(Date dt) {
        int week = getWeekOfDate(dt);
        if(week == 0){
            return "周一";
        }else if(week == 1){
            return "周二";
        }else if(week == 2){
            return "周三";
        }else if(week == 3){
            return "周四";
        }else if(week == 4){
            return "周五";
        }else if(week == 5){
            return "周六";
        }else if(week == 6){
            return "周日";
        }
        return "";
    }
    /**
     * yyyy-MM-dd HH:mm:ss 格式转为日期 星期 时间的格式
     * @param time
     * @format 时间格式
     * @param thisYear 本年
     */
    public static String formatToPickerTime(String time, String format, String thisYear){
        try {
            time = time.trim();
            Date date = null;
            try {
                date = new SimpleDateFormat(format).parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (date == null) return time;
            else {
                String newformatDate =  new SimpleDateFormat("yyyy年MM月dd日 HH点mm分").format(date);
                String nfd1 = newformatDate.substring(0, newformatDate.indexOf(" ")).trim();
                if(nfd1.startsWith(thisYear)){//今年
                    nfd1 = nfd1.replace(thisYear + "年", "");
                }
                String nfd2 = newformatDate.substring(newformatDate.indexOf(" ") + 1).trim();
                return nfd1 + " " + getWeekDesc(date) + " " + nfd2;
            }
        }catch (Exception e){
            return time;
        }
    }
}
